package jogo.gwt.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.TouchMoveEvent;
import com.google.gwt.event.dom.client.TouchMoveHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class Main implements EntryPoint {
	private Canvas canvas;
	private Context2d context;
	private static final int WIDTH = 640;
	private static final int HEIGHT = 480;

	private Retangulo base = new Retangulo(190, 430, 25, 100);
	private Retangulo bola = new Retangulo(0, 0, 25, 25);
	private int dirX = 3;
	private int dirY = 3;

	private boolean keyLeft = false;
	private boolean keyRight = false;
	
	private ImageElement imgFundo = ImageElement.as(new Image("fundo.png").getElement());
	private ImageElement imgBola = ImageElement.as(new Image("bola.png").getElement());
	private ImageElement imgBase = ImageElement.as(new Image("base.png").getElement());
	
	private int frames = 0;
	private int fps = 0;
	private long tempo = System.currentTimeMillis();


	@Override
	public void onModuleLoad() {
		canvas = Canvas.createIfSupported();
		if (canvas == null) {
			RootPanel.get().add(new Label("Navegador sem suporte ao Canvas"));
			return;
		}
		RootPanel.get().add(canvas);
		context = canvas.getContext2d();

		// retira a margem e barra de rolagem
		Window.setMargin("0px");
		Window.enableScrolling(false);

		// define o tamanho do elemento canvas
		canvas.setWidth(Window.getClientWidth() + "px");
		canvas.setHeight(Window.getClientHeight() + "px");

		// define resolu��o do objeto canvas
		canvas.setCoordinateSpaceWidth(WIDTH);
		canvas.setCoordinateSpaceHeight(HEIGHT);

		Timer timer = new Timer() {
			@Override
			public void run() {
				update();
			}
		};
		timer.scheduleRepeating(33);

		initKeyHandlers();
		initMouseHandlers();
		initTouchHandlers();
	}

	public void update() {
		frames++;
		if (tempo>System.currentTimeMillis()) {
			tempo = System.currentTimeMillis();
		}
		if (System.currentTimeMillis()-1000>=tempo) {
			tempo = System.currentTimeMillis();
			fps = frames;
			frames = 0;
		}
		
		
		bola.setX(bola.getX() + dirX);
		bola.setY(bola.getY() + dirY);
		if (bola.getX() + bola.getWidth() >= WIDTH)
			dirX *= -1;
		if (bola.getX() <= 0)
			dirX *= -1;
		if (bola.getY() <= 0)
			dirY *= -1;
		if (base.colide(bola))
			dirY *= -1;
		if (bola.getY() + bola.getHeight() >= HEIGHT) {
			Window.alert("Você Perdeu!");
			bola.setX(0);
			bola.setY(0);
			dirX = Math.abs(dirX);
			dirY = Math.abs(dirY);
		}

		if ((keyLeft) && (base.getX() > 0)) {
			base.setX(base.getX() - 10);
		}
		if ((keyRight) && (base.getX() + base.getWidth() < WIDTH)) {
			base.setX(base.getX() + 10);
		}

		draw();
	}

	public void draw() {
		context.drawImage(imgFundo, 0, 0);

		context.drawImage(imgBase, base.getX(), base.getY());
		context.drawImage(imgBola, bola.getX(), bola.getY());

		context.setFont("bold 20px sans-serif");
		CssColor cor = CssColor.make("rgba(255,255,255,1)");
		context.setFillStyle(cor);
		context.fillText(fps + " fps", 10, 20);
	}

	public void initKeyHandlers() {
		canvas.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(KeyDownEvent event) {
				int key = event.getNativeKeyCode();
				if (key == 37) {
					keyLeft = true;
				} else if (key == 39) {
					keyRight = true;
				}
			}
		});
		canvas.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				int key = event.getNativeKeyCode();
				if (key == 37) {
					keyLeft = false;
				} else if (key == 39) {
					keyRight = false;
				}
			}
		});
	}

	public void initMouseHandlers() {
		canvas.addMouseMoveHandler(new MouseMoveHandler() {
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				int x = event.getX();
				x = x * WIDTH / Window.getClientWidth();
				base.setX((x - base.getWidth() / 2));
			}
		});
	}

	public void initTouchHandlers() {
		canvas.addTouchMoveHandler(new TouchMoveHandler() {
			@Override
			public void onTouchMove(TouchMoveEvent event) {
				int x = event.getTouches().get(0).getClientX();
				x = x * WIDTH / Window.getClientWidth();
				base.setX((x - base.getWidth() / 2));
			}
		});
	}

}
